package org.stone.project.ncCheck.utils;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.stone.project.ncCheck.prop.IpProp;

import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class IpUtil {
    @Autowired
    private IpProp ipProp;

    public void addAddress(List<String> ipv4List,List<String> ipv6List) throws SocketException {
        Boolean hasNetHard = false;
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        log.info("网卡信息遍历开始");
        while (networkInterfaces.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (networkInterface.getDisplayName() != null && networkInterface.getDisplayName().equals(ipProp.getHardWareName())){
                hasNetHard = true;
                // 遍历每个网络接口下的所有IP地址
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

                while (inetAddresses.hasMoreElements()){
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()){
                        if (inetAddress instanceof Inet4Address){
                            if (inetAddress.getHostAddress() != null && inetAddress.getHostAddress().indexOf(ipProp.getIpv4Prefix()) == 0){
                                ipv4List.add(inetAddress.getHostAddress());
                            }
                        }
                        if(inetAddress instanceof Inet6Address){
                            if (inetAddress.getHostAddress() != null && inetAddress.getHostAddress().indexOf(ipProp.getIpv6Prefix()) == 0){
                                ipv6List.add(inetAddress.getHostAddress());
                            }
                        }
                    }
                }
            }
            log.info("网卡名称{}为遍历",networkInterface.getDisplayName());
        }
        log.info("网卡信息遍历结束");

        if (!hasNetHard)
            log.error("网卡未匹配上");
        if (ipv4List.size() > 1 || ipv6List.size() > 1){
            log.warn("ipv4网卡地址数量为多个{}",ipv4List);
            log.warn("ipv6网卡地址数量为多个{}",ipv6List);
        }else if(ipv4List.size() ==0 && ipv6List.size() == 0){
            log.error("网卡ip未查询到");
        }else {
            log.info("网卡信息正常");
        }
    }












    public static void main(String[] args) {
        try {
            // 获取所有的网络接口
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                // 遍历每个网络接口下的所有IP地址
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    // 打印出IP地址，这里排除了回环地址（如127.0.0.1）
                    if (!inetAddress.isLoopbackAddress()) {
                        System.out.println("Interface: " + networkInterface.getName() + ", IP: " + inetAddress.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            System.err.println("SocketException occurred: " + e.getMessage());
        }
    }
}
